﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WebParts;


namespace SilverlightClock_MVP
{
	public partial class Page : UserControl
	{
        #region Properties
        public TimeSpan UtcOffsetTicks { get; set; }
        #endregion

        public Page()
		{
            InitializeComponent();

            MakeContent();
		}

        public Page(IDictionary<string, string> Parameters)
        {
            InitializeComponent();

            if (Parameters.ContainsKey("ClockTitle"))
            {
                this.AnalogClockControl_v2.ClockTitle = (string.IsNullOrEmpty(Parameters["ClockTitle"])) ? "Clock Title" : Parameters["ClockTitle"].Replace('|', ',');
            }

            if (Parameters.ContainsKey("ShowDigitalClock"))
            {
                bool b;
                bool.TryParse(Parameters["ShowDigitalClock"], out b);
                this.AnalogClockControl_v2.ShowDigitalClock = b;
            }

            if (Parameters.ContainsKey("ShowSecondHand"))
            {
                bool b;
                bool.TryParse(Parameters["ShowSecondHand"], out b);
                this.AnalogClockControl_v2.ShowSecondHand = b;
            }

            if (Parameters.ContainsKey("ShowMilliHand"))
            {
                bool b;
                bool.TryParse(Parameters["ShowMilliHand"], out b);
                this.AnalogClockControl_v2.ShowMilliHand = b;
            }

            if (Parameters.ContainsKey("ShowTenthsHand"))
            {
                bool b;
                bool.TryParse(Parameters["ShowTenthsHand"], out b);
                this.AnalogClockControl_v2.ShowTenthsHand = b;
            }


            if (Parameters.ContainsKey("UtcOffsetTicks"))
            {
                long offsetTicks;
                if (long.TryParse(Parameters["UtcOffsetTicks"], out offsetTicks))
                {
                    UtcOffsetTicks = new TimeSpan(offsetTicks);
                }
            }
            else
            {
                UtcOffsetTicks = TimeZoneInfo.Local.BaseUtcOffset;
            }

            MakeContent();
        }

        private void MakeContent()
        {
            IClockModel clockModel = Clock.Create(UtcOffsetTicks);
            clockModel.ClockData = new ClockData().Update(DateTime.UtcNow);
            this.AnalogClockControl_v2.ClockUtcOffset = UtcOffsetTicks;
            
            ClockPresenter presenter = new ClockPresenter(this.AnalogClockControl_v2, clockModel);
            presenter.Response += new Callback(this.AnalogClockControl_v2.Update);
            
            clockModel.Start();
        }

	}
}
